package xyz.msj.hrm.web.controller;

import com.baomidou.mybatisplus.plugins.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import xyz.msj.hrm.domain.Course;
import xyz.msj.hrm.dto.CourseDTO;
import xyz.msj.hrm.query.CourseQuery;
import xyz.msj.hrm.service.ICourseService;
import xyz.msj.hrm.util.AjaxResult;
import xyz.msj.hrm.util.PageList;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/course")
public class CourseController {
    @Autowired
    public ICourseService courseService;


    /**
     * 前台查询
     * @param courseQuery
     * @return Ajaxresult转换结果
     */
    @RequestMapping(value="/queryCourses",method= RequestMethod.POST)
    public AjaxResult queryCourses(@RequestBody CourseQuery courseQuery) {
        try {
            return courseService.queryCourses(courseQuery);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("查询课程失败！" + e.getMessage());
        }
    }/**
     * 资源上传
     * @param param
     * @return Ajaxresult转换结果
     */
    @RequestMapping(value="/addPhoto",method= RequestMethod.POST)
    public AjaxResult addPhoto(@RequestBody Map<String,String> param) {
        try {
            courseService.addPhoto(param);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("上传资源失败！" + e.getMessage());
        }
    }
    /**
     * 下线
     * @param id  id
     * @return Ajaxresult转换结果
     */
    @RequestMapping(value="/offLineCourse/{id}",method= RequestMethod.POST)
    public AjaxResult offLineCourse(@PathVariable Long id){
        try {
            courseService.offLineCourse(id);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("下线失败！"+e.getMessage());
        }
    }/**
     * 上线
     * @param id  id
     * @return Ajaxresult转换结果
     */
    @RequestMapping(value="/onLineCourse/{id}",method= RequestMethod.POST)
    public AjaxResult onLineCourse(@PathVariable Long id){
        try {
            courseService.onLineCourse(id);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("上线失败！"+e.getMessage());
        }
    }
    /**
     * 保存新添加的课程
     * @param courseDTO  传递的实体
     * @return Ajaxresult转换结果
     */
    @RequestMapping(value="/saveOrUpdate",method= RequestMethod.POST)
    public AjaxResult saveOrUpdate(@RequestBody CourseDTO courseDTO){
        try {
            courseService.saveOrUpdate(courseDTO);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("保存对象失败！"+e.getMessage());
        }
    }
    /**
     * 保存和修改公用的
     * @param course  传递的实体
     * @return Ajaxresult转换结果
     */
    @RequestMapping(value="/save",method= RequestMethod.POST)
    public AjaxResult save(@RequestBody Course course){
        try {
            if(course.getId()!=null){
                courseService.updateById(course);
            }else{
                courseService.insert(course);
            }
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("保存对象失败！"+e.getMessage());
        }
    }

    /**
     * 删除对象信息
     * @param id
     * @return
     */
    @RequestMapping(value="/{id}",method=RequestMethod.DELETE)
    public AjaxResult delete(@PathVariable("id") Long id){
        try {
            courseService.deleteById(id);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("删除对象失败！"+e.getMessage());
        }
    }

    //获取用户
    @RequestMapping(value = "/{id}",method = RequestMethod.GET)
    public Course get(@PathVariable("id")Long id)
    {
        return courseService.selectById(id);
    }


    /**
     * 查看所有的员工信息
     * @return
     */
    @RequestMapping(value = "/list",method = RequestMethod.GET)
    public List<Course> list(){

        return courseService.selectList(null);
    }


    /**
     * 分页查询数据
     *
     * @param query 查询对象
     * @return PageList 分页对象
     */
    @RequestMapping(value = "/pagelist",method = RequestMethod.POST)
    public PageList<Course> json(@RequestBody CourseQuery query)
    {
        Page<Course> page = new Page<Course>(query.getPage(),query.getRows());
        page = courseService.selectPage(page);
        return new PageList<Course>(page.getTotal(),page.getRecords());
    }
}
